package com.apes.fn.business.insurance.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.business.insurance.modle.CommissionReconciliation;
import com.apes.fn.business.insurance.modle.InAccountInformation;
import com.apes.fn.business.insurance.repository.CommissionReconciliationRepository;
import com.apes.fn.business.insurance.repository.InAccountInformationRepostitory;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.Tools;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: apes-server
 * @description
 * @author: czs
 * @create: 2022-06-17 09:55
 **/
@Service("inAccountInformationService")
public class InAccountInformationService extends DomainService {

    @Autowired
    CommissionReconciliationRepository commissionReconciliationRepository;
    @Autowired
    InAccountInformationRepostitory inAccountInformationRepostitory;

    /**
     * 佣金对账单生产对账信息
     * @param request
     * @return
     */
    public CommissionReconciliation create(SimpleRequest request){
        JSONObject jo = request.getJO();
        String id = (String) jo.remove("id");
        InAccountInformation inAccountInformation = Tools.map(jo,InAccountInformation.class);
        CommissionReconciliation reconciliation = commissionReconciliationRepository.findOne(id);
        inAccountInformation.setCommissionReconciliation(reconciliation);
        try {
            PartyRole person = request.getPerson();
            inAccountInformation.setOperatorPeople(person.getName());
        } catch (Exception e) {
            throw new RuntimeException("获取当前登录角色失败");
        }
        inAccountInformation = inAccountInformationRepostitory.saveAndFlush(inAccountInformation);
        //返回信息
        CommissionReconciliation commissionReconciliation = commissionReconciliationRepository.findOne(id);
        commissionReconciliation.setState(inAccountInformation.getState());

        return commissionReconciliationRepository.saveAndFlush(commissionReconciliation);
    }
}
